using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class BicycleTypesController : Controller
    {
        public BicycleTypesController(IRepository<BicycleType> bicycleTypeRepository) {
            Check.Require(bicycleTypeRepository != null, "bicycleTypeRepository may not be null");

            this.bicycleTypeRepository = bicycleTypeRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<BicycleType> bicycleTypes = bicycleTypeRepository.GetAll();
            return View(bicycleTypes);
        }

        [Transaction]
        public ActionResult Show(int id) {
            BicycleType bicycleType = bicycleTypeRepository.Get(id);
            return View(bicycleType);
        }

        public ActionResult Create() {
            BicycleTypeFormViewModel viewModel = BicycleTypeFormViewModel.CreateBicycleTypeFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(BicycleType bicycleType) {
            if (ViewData.ModelState.IsValid && bicycleType.IsValid()) {
                bicycleTypeRepository.SaveOrUpdate(bicycleType);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleType was successfully created.";
                return RedirectToAction("Index");
            }

            BicycleTypeFormViewModel viewModel = BicycleTypeFormViewModel.CreateBicycleTypeFormViewModel();
            viewModel.BicycleType = bicycleType;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            BicycleTypeFormViewModel viewModel = BicycleTypeFormViewModel.CreateBicycleTypeFormViewModel();
            viewModel.BicycleType = bicycleTypeRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(BicycleType bicycleType) {
            BicycleType bicycleTypeToUpdate = bicycleTypeRepository.Get(bicycleType.Id);
            TransferFormValuesTo(bicycleTypeToUpdate, bicycleType);

            if (ViewData.ModelState.IsValid && bicycleType.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleType was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                bicycleTypeRepository.DbContext.RollbackTransaction();

				BicycleTypeFormViewModel viewModel = BicycleTypeFormViewModel.CreateBicycleTypeFormViewModel();
				viewModel.BicycleType = bicycleType;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(BicycleType bicycleTypeToUpdate, BicycleType bicycleTypeFromForm) {
			bicycleTypeToUpdate.Name = bicycleTypeFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The bicycleType was successfully deleted.";
            BicycleType bicycleTypeToDelete = bicycleTypeRepository.Get(id);

            if (bicycleTypeToDelete != null) {
                bicycleTypeRepository.Delete(bicycleTypeToDelete);

                try {
                    bicycleTypeRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the bicycleType from being deleted. " +
						"Another item likely depends on this bicycleType.";
                    bicycleTypeRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The bicycleType could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the BicycleType form for creates and edits
		/// </summary>
        public class BicycleTypeFormViewModel
        {
            private BicycleTypeFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static BicycleTypeFormViewModel CreateBicycleTypeFormViewModel() {
                BicycleTypeFormViewModel viewModel = new BicycleTypeFormViewModel();
                
                return viewModel;
            }

            public BicycleType BicycleType { get; internal set; }
        }

        private readonly IRepository<BicycleType> bicycleTypeRepository;
    }
}
